package com.ruoyi.project.jiaqin.quotation.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.jiaqin.quotation.domain.Quotation;
import com.ruoyi.project.jiaqin.quotation.service.IQuotationService;
import com.ruoyi.project.jiaqin.quotationclass.domain.QuotationClass;
import com.ruoyi.project.jiaqin.quotationclass.service.IQuotationClassService;
import com.ruoyi.project.jiaqin.val.domain.ClassAttrVal;
import com.ruoyi.project.jiaqin.val.service.IClassAttrValService;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.service.IMemberService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 行情Controller
 *
 * @author admin
 * @date 2020-12-21
 */
@Controller
@RequestMapping("/jiaqin/quotation")
public class QuotationController extends BaseController {
    private String prefix = "jiaqin/quotation";

    @Autowired
    private IQuotationService quotationService;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private IQuotationClassService quotationClassService;
    @Autowired
    private IClassAttrValService classAttrValService;

    @RequiresPermissions("jiaqin:quotation:view")
    @GetMapping()
    public String quotation()
    {
        return prefix + "/quotation";
    }

    /**
     * 查询行情列表
     */
    @RequiresPermissions("jiaqin:quotation:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Quotation quotation)
    {
        startPage();
        quotation.setDelFlag(0);
        List<Quotation> list = quotationService.selectQuotationList(quotation);
        for (Quotation Q : list) {
            Member member = memberService.selectMemberById(Q.getMemberId());
            if (member != null) {
                Q.setMemberId(member.getUsername());
            }
            QuotationClass classIdTwo = quotationClassService.selectQuotationClassById(Q.getClassIdTwo());
            if (classIdTwo != null) {
                Q.setClassIdTwo(classIdTwo.getName());
            }
            QuotationClass classIdThr = quotationClassService.selectQuotationClassById(Q.getClassIdThr());
            if (classIdThr != null) {
                Q.setClassIdThr(classIdThr.getName());
            }
            ClassAttrVal attrVal = classAttrValService.selectClassAttrValById(Q.getAttrVals());
            if (attrVal != null) {
                Q.setAttrVals(attrVal.getAttrVal());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出行情列表
     */
    @RequiresPermissions("jiaqin:quotation:export")
    @Log(title = "行情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Quotation quotation)
    {
        List<Quotation> list = quotationService.selectQuotationList(quotation);
        ExcelUtil<Quotation> util = new ExcelUtil<Quotation>(Quotation.class);
        return util.exportExcel(list, "quotation");
    }

    /**
     * 新增行情
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存行情
     */
    @RequiresPermissions("jiaqin:quotation:add")
    @Log(title = "行情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Quotation quotation)
    {
        return toAjax(quotationService.insertQuotation(quotation));
    }

    /**
     * 修改行情
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        Quotation quotation = quotationService.selectQuotationById(id);
        mmap.put("quotation", quotation);
        return prefix + "/edit";
    }

    /**
     * 修改保存行情
     */
    @RequiresPermissions("jiaqin:quotation:edit")
    @Log(title = "行情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Quotation quotation)
    {
        return toAjax(quotationService.updateQuotation(quotation));
    }

    /**
     * 删除行情
     */
    @RequiresPermissions("jiaqin:quotation:remove")
    @Log(title = "行情", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(quotationService.deleteQuotationByIds(ids));
    }
}
